package dragon.modules.system.user.dao;

import java.util.ArrayList;
import java.util.List;

import dragon.core.db.DML;
import dragon.core.exception.ExceptionHandler;
import dragon.core.system.dao.UserGroupDao;
import dragon.core.system.po.SysUserGroup;
import dragon.core.system.po.SysUsermanagementroles;
import dragon.core.system.role.Roles;
import dragon.core.util.GlobalParameter;
import dragon.core.util.StringUtils;

public class UserGroupDaoImpl implements UserGroupDao {

	/*
	 * (non-Javadoc)
	 * 
	 * @see dragon.modules.system.user.dao.UserGroupDao#getAdministrators()
	 */
	@Override
	public SysUserGroup getAdministrators() throws ExceptionHandler {
		if (GlobalParameter.Administrators == null) {
			GlobalParameter.Administrators = loadUserGroupByAlias(Roles.Administrators.name());
		}
		return GlobalParameter.Administrators;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see dragon.modules.system.user.dao.UserGroupDao#getUsers()
	 */
	@Override
	public SysUserGroup getUsers() throws ExceptionHandler {
		if (GlobalParameter.Users == null) {
			GlobalParameter.Users = loadUserGroupByAlias(Roles.Users.name());
		}
		return GlobalParameter.Users;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see dragon.modules.system.user.dao.UserGroupDao#getGuests()
	 */
	@Override
	public SysUserGroup getGuests() throws ExceptionHandler {
		if (GlobalParameter.Guests == null) {
			GlobalParameter.Guests = loadUserGroupByAlias(Roles.Guests.name());
		}
		return GlobalParameter.Guests;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see dragon.modules.system.user.dao.UserGroupDao#loadUserGroupByAlias(java.lang.String)
	 */
	@Override
	public SysUserGroup loadUserGroupByAlias(String alias) throws ExceptionHandler {
		if (StringUtils.isEmpty(alias))
			return null;
		return DML.loadObject(SysUserGroup.class, "where upper(alias)=?", alias.toUpperCase());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see dragon.modules.system.user.dao.UserGroupDao#loadAllUserGroup()
	 */
	@Override
	public List<SysUserGroup> loadAllUserGroup() throws ExceptionHandler {
		return DML.query(new ArrayList<SysUserGroup>(), SysUserGroup.class, "select * from " + SysUserGroup.tableName + " order by ordernum asc");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see dragon.core.system.dao.UserGroupDao#loadUserManagementGroup(int)
	 */
	@Override
	public List<SysUserGroup> loadUserManagementGroup(int uid) throws ExceptionHandler {
		return DML.query(new ArrayList<SysUserGroup>(), SysUserGroup.class, "select A.* from " + SysUserGroup.tableName + " A left join " + SysUsermanagementroles.tableName
				+ " B on A.id=B.usergroupid where B.userid=" + uid + " order by ordernum asc");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see dragon.modules.system.user.dao.UserGroupDao#getUserGroupsByIds(java.lang.String)
	 */
	@Override
	public List<SysUserGroup> getUserGroupsByIds(String ids) throws ExceptionHandler {
		String sql = "select * from " + SysUserGroup.tableName + " where id in (" + ids + ")";
		return DML.query(new ArrayList<SysUserGroup>(), SysUserGroup.class, sql);
	}

}
